package beans;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import menagement.UserMgr;
import menagement.dto.UserDTO;

@ManagedBean(name="registerBean")
@RequestScoped
public class RegisterBean {
	
	@EJB
	private UserMgr userMgr;

	private UserDTO user;
	
	public RegisterBean() {
		user = new UserDTO();
	}

	public UserDTO getUser() {
		return user;
	}

	public void setUser(UserDTO user) {
		this.user = user;
	}
	
	public String register() {
		Boolean valido=userMgr.save(user);
		if(valido){
			FacesMessage msg = new FacesMessage("Utente registrato con successo!", user.getEmail());
	        FacesContext.getCurrentInstance().addMessage(null, msg);
			return "home?faces-redirect=true";
		}
		else{
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Esiste gia un utente con la mail selezionata:", user.getEmail());
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return null;
		}
		
	}
}
